//
//  SNPlanListCell.m
//  ZhuoxinAgriculture
//
//  Created by stone on 16/12/25.
//  Copyright © 2016年 stone. All rights reserved.
//

#import "SNCheckPlanViewController.h"
#import "SNCompilePlanViewController.h"
#import "SNPlanListCell.h"
#import "SNPlanListButton.h"
@interface SNPlanListCell()
@property (weak, nonatomic) IBOutlet UIView *bgView;
@property (weak, nonatomic) IBOutlet UILabel *planIdentifierLabel;
@property (weak, nonatomic) IBOutlet UILabel *seedNameLabel;
@property (weak, nonatomic) IBOutlet UILabel *startTime;
@property (weak, nonatomic) IBOutlet UILabel *plantingArea;
@property (weak, nonatomic) IBOutlet UIImageView *typeImageView;
@property (weak, nonatomic) IBOutlet SNPlanListButton *leftButton;
@property (weak, nonatomic) IBOutlet SNPlanListButton *rightButton;

@end
@implementation SNPlanListCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    self.bgView.layer.contents = (__bridge id)[UIImage imageNamed:@"01-list-base"].CGImage;
    self.bgView.backgroundColor = kNormalBackgroundColor;
}
-(void)setProductionPlanListResult:(SNProductionPlanListResult *)productionPlanListResult{
    _productionPlanListResult = productionPlanListResult;
    
    self.planIdentifierLabel.text = [NSString stringWithFormat:@"计划编号: %@",productionPlanListResult.planNo];
    self.seedNameLabel.text = productionPlanListResult.cropTypeName;
    self.startTime.text = [NSString stringWithFormat:@"种植时间: %@",productionPlanListResult.startTime];
    self.plantingArea.text = [NSString stringWithFormat:@"种植面积: %@ 公顷",productionPlanListResult.plantingArea];

    [self checkStatus:productionPlanListResult.status];
}

-(void)setModel:(SNPlanListModel *)model{
    _model = model;
    
    self.planIdentifierLabel.text = [NSString stringWithFormat:@"计划编号: %@",model.planNo];
    
    switch ([model.cropType integerValue]) {
        case 0:
            model.seedName = @"玉米";
            break;
        case 1:
            model.seedName = @"大豆";
            break;
        case 2:
            model.seedName = @"小麦";
            break;
        default:
            break;
    }

    self.seedNameLabel.text = model.seedName;
    self.startTime.text = [NSString stringWithFormat:@"种植时间: %@",model.startTime];
    self.plantingArea.text = [NSString stringWithFormat:@"种植面积: %@ 公顷",model.plantingArea];

    
    [self checkStatus:model.status];
}
- (void)checkStatus:(NSString *)status
{
    
    /** 角色, 1:小农 2:农场员工 3:散户 4:决策者 5:联社业务代表 6:组织业务代表 */
    switch (kUserDetailInfo.userType.integerValue) {
        case 1:
        case 2:
        case 3:
        {
            switch (status.integerValue) {
                case 0:
                    self.typeImageView.image = [UIImage imageNamed:@"icon_daitijiao"];
                    self.leftButton.hidden = YES;
                    
                    [self.rightButton setImage:[UIImage imageNamed:@"04-review"] forState:UIControlStateNormal];
                    [self.rightButton setTitle:@"查看" forState:UIControlStateNormal];
                    break;
                case 1:
                    self.typeImageView.image = [UIImage imageNamed:@"icon_daishenhe"];
                    self.leftButton.hidden = YES;
                    
                    [self.rightButton setImage:[UIImage imageNamed:@"04-review"] forState:UIControlStateNormal];
                    [self.rightButton setTitle:@"查看" forState:UIControlStateNormal];
                    break;
                    break;
                case 2:
                    self.typeImageView.image = [UIImage imageNamed:@"icon_yitongguo"];
                    self.leftButton.hidden = YES;
                    
                    [self.rightButton setImage:[UIImage imageNamed:@"04-review"] forState:UIControlStateNormal];
                    [self.rightButton setTitle:@"查看" forState:UIControlStateNormal];
                    break;
                default:
                    break;
            }
        }
            return;
        default:
            break;
    }
    
    
    
    switch (status.integerValue) {
        case 0:
            self.typeImageView.image = [UIImage imageNamed:@"icon_daitijiao"];
            [self.leftButton setImage:[UIImage imageNamed:@"04-edit"] forState:UIControlStateNormal];
            [self.leftButton setTitle:@"编辑" forState:UIControlStateNormal];
            self.leftButton.hidden = NO;
            
            [self.rightButton setImage:[UIImage imageNamed:@"01-submit"] forState:UIControlStateNormal];
            [self.rightButton setTitle:@"提交" forState:UIControlStateNormal];
            break;
        case 1:
            self.typeImageView.image = [UIImage imageNamed:@"icon_daishenhe"];
            [self.leftButton setImage:[UIImage imageNamed:@"04-recall"] forState:UIControlStateNormal];
            [self.leftButton setTitle:@"撤回" forState:UIControlStateNormal];
            self.leftButton.hidden = NO;
            
            [self.rightButton setImage:[UIImage imageNamed:@"04-review"] forState:UIControlStateNormal];
            [self.rightButton setTitle:@"查看" forState:UIControlStateNormal];
            break;
        case 2:
            self.typeImageView.image = [UIImage imageNamed:@"icon_yitongguo"];
            self.leftButton.hidden = YES;
            
            [self.rightButton setImage:[UIImage imageNamed:@"04-review"] forState:UIControlStateNormal];
            [self.rightButton setTitle:@"查看" forState:UIControlStateNormal];
            break;
        default:
            break;
    }
}
- (IBAction)leftButtonClicked:(SNPlanListButton *)sender {
    if([sender.currentTitle isEqualToString:@"编辑"]){
        NSLog(@"%@ | %s",@"编辑",__func__);
        /** 跳转编辑生产计划 */
        SNCompilePlanViewController * vc = [SNCompilePlanViewController new];
        vc.rowCount = 1;
        vc.productionPlanListResult = self.productionPlanListResult;
        vc.plantingDecisionMakerResult = self.plantingDecisionMakerResult;
        vc.indexPath = self.indexPath;
        [self.viewController.navigationController pushViewController:vc animated:YES];
        
    }else if([sender.currentTitle isEqualToString:@"撤回"]){
        NSLog(@"%@ | %s",@"撤回",__func__);
        [self addCancelAlertController];
    }
}
- (IBAction)rightButtonClicked:(SNPlanListButton *)sender {
    if([sender.currentTitle isEqualToString:@"提交"]){
        NSLog(@"%@ | %s",@"提交",__func__);
        [self addCommitAlertController];
    }else if([sender.currentTitle isEqualToString:@"查看"]){
         NSLog(@"%@ | %s",@"查看",__func__);
        SNCheckPlanViewController * vc = [SNCheckPlanViewController new];
        vc.model = self.model;
        vc.productionPlanListResult = self.productionPlanListResult;
        vc.plantingDecisionMakerResult = self.plantingDecisionMakerResult;
        [self.viewController.navigationController pushViewController:vc animated:YES];
    }
}
//sn_note:=========  ============================ stone 🐳 ===========/
- (void)addCommitAlertController
{
    NSString * title = @"";
    NSString * message = @"是否确定提交该生产计划 ?";
    NSString * actionButtonTitle1 = @"取消";
    NSString * actionButtonTitle2 = @"确定";
    
    UIAlertController * alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction * action1 = [UIAlertAction actionWithTitle:actionButtonTitle1 style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        // 取消之后需要 处理的code
        
    }];
    UIAlertAction * action2 = [UIAlertAction actionWithTitle:actionButtonTitle2 style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        // 确定之后需要 处理的code
        [[ZXHTTPManager sharedManager] plancommitWithPlanNo:self.productionPlanListResult.planNo operatorName:self.productionPlanListResult.operatorName completed:^(NSDictionary *jsonDict, NSError *error) {
            
            logError;
            
            if ([jsonDict[@"status"] integerValue] == 0) {
                !self.reloadDataOfTableView ?: self.reloadDataOfTableView();
            }
            
        }];
    }];
    
    [alertController addAction:action1];
    [alertController addAction:action2];
    
    [self.viewController presentViewController:alertController animated:YES completion:nil];

}
- (void)addCancelAlertController
{
    NSString * title = @"";
    NSString * message = @"是否确定撤回该生产计划 ?";
    NSString * actionButtonTitle1 = @"取消";
    NSString * actionButtonTitle2 = @"确定";
    
    UIAlertController * alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction * action1 = [UIAlertAction actionWithTitle:actionButtonTitle1 style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        // 取消之后需要 处理的code
        
    }];
    UIAlertAction * action2 = [UIAlertAction actionWithTitle:actionButtonTitle2 style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        // 确定之后需要 处理的code
        [[ZXHTTPManager sharedManager] planrecallWithPlanNo:self.productionPlanListResult.planNo operatorName:kUserInfo.userId completed:^(NSDictionary *jsonDict, NSError *error) {
           
            logError;
            
            if ([jsonDict[@"status"] integerValue] == 0) {
                !self.reloadDataOfTableView ?: self.reloadDataOfTableView();
            }
            
        }];
    }];
    
    [alertController addAction:action1];
    [alertController addAction:action2];
    
    [self.viewController presentViewController:alertController animated:YES completion:nil];
}
@end
